/*
 * FileName：SysResCtrl.java 
 * <p>
 * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.primary.sys.ctrl;

import java.util.ArrayList;
import java.util.List;

import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.StringUtil;
import com.smart.primary.sys.service.ResService;
import com.smart.primary.sys.vo.Res;
import com.smart.primary.sys.vo.User;
import com.smart.primary.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

/**
 * 资源菜单信息表 Controller层
 *
 * @author hermit
 * @date 2018-02-23 11:02:18
 */
@Controller
@RequestMapping("/sys/res")
public class ResCtrl extends BaseCtrl {
    
    @Autowired
    private ResService sysResService;

    /**
     * 获取页面分页数据集合
     *
     * @author hermit
     * @date 2018-02-23 11:02:18
     * @param sysRes
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult list(Res sysRes)
        throws Exception {
        List<Res> list = sysResService.queryListByPage(sysRes);
        return getResult(sysRes, list);
    }
    
    /**
     * 获取页面资源树
     *
     * @author hermit
     * @date 2018-02-23 11:02:18
     * @param sysRes
     * @return
     */
    @RequestMapping("/restree")
    @ResponseBody
    public AjaxResult restree(Res sysRes)
        throws Exception {
        List<Object> resList = this.sysResService.getSysResTreeList(sysRes);
        return AjaxResult.success(resList);
    }
    
    /**
     * 获取页面资源树
     *
     * @author hermit
     * @date 2018-02-23 11:02:18
     * @param sysRes
     * @return
     */
    @RequestMapping("/getres")
    @ResponseBody
    public AjaxResult getres(Res sysRes)
        throws Exception {
        Res res = this.sysResService.queryObject(sysRes);
        return AjaxResult.success(res);
    }
    
    /**
     * 数据保存(新增或修改)
     *
     * @author hermit
     * @date 2018-02-23 11:02:18
     * @param sysRes
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(Res sysRes)
        throws Exception {

        Res queryResCode = new Res();
        queryResCode.setResId(sysRes.getResId());
        queryResCode.setResCode(sysRes.getResCode());
        // 资源编码(唯一)验证
        int resCodeCount = this.sysResService.queryCount(queryResCode);
        Res queryResName = new Res();
        queryResName.setResId(sysRes.getResId());
        queryResName.setParentId(sysRes.getParentId());
        queryResName.setResName(sysRes.getResName());
        // 同一个目录下资源名称唯一
        int resNameCount = this.sysResService.queryCount(queryResName);
        if (resNameCount > 0) {
            return AjaxResult.failure("该资源名称已存在");
        } else if(resCodeCount > 0){
            return AjaxResult.failure("该资源编码已存在");
        } else {
            // 从session中取出userId
            // 获取当前登录用户
            User sysUser = (User) WebUtils.getSessionAttribute(request, Constants.SESSION_USER);
            String userId = null;
            if (sysUser != null) {
                userId = sysUser.getUserId();
            }
            if (StringUtil.isNotEmpty(sysRes.getResId())) {
                sysRes.setUpdateUserId(userId);
                Integer m = sysResService.update(sysRes);
                if (m > 0) {
                    return AjaxResult.updateSuccess();
                } else {
                    return AjaxResult.failure("信息修改失败");
                }
            } else {
                // 新增操作
                Res queryRes = new Res();
                queryRes.setResId(sysRes.getParentId());
                Res sysResResult = this.sysResService.queryObject(queryRes);
                // 如果是叶子节点, 需要修改成非叶子节点
                if (sysResResult != null && sysResResult.getIsLeaf() == 1) {
                    queryRes.setIsLeaf(0);
                    sysResService.update(queryRes);
                }
                sysRes.setCreateUserId(userId);
                sysRes.setUpdateUserId(userId);
                Integer n = sysResService.insert(sysRes);
                if (n > 0) {
                    return AjaxResult.saveSuccess();
                } else {
                    return AjaxResult.failure("信息添加失败");
                }
            }

        }
    }

    /**
     * 数据删除
     *
     * @author hermit
     * @date 2018-02-23 11:02:18
     * @param sysRes
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Res sysRes)
        throws Exception {
        // 查询子级数量
        int childCount = this.sysResService.getChildCount(sysRes);
        if (childCount > 0) {
            return AjaxResult.failure("请先删除子节点");
        } else {
            Integer n = sysResService.delete(sysRes.getResId());
            if (n > 0) {
                return AjaxResult.deleteSuccess();
            } else {
                return AjaxResult.failure("信息删除失败");
            }
        }
    }

    /**
     * 获取导航菜单列表
     *
     * @return list
     * @throws Exception the exception
     * @author whz
     * @date 2018 -02-24 15:19:46
     */
    @RequestMapping("/getMenuList")
    @ResponseBody
    public AjaxResult getMenuList()
        throws Exception {
         //获取当前登录用户
         User sysUser = SessionUtil.getUser(request, response);
         String userId = null;

        List<Res> objectList=new ArrayList<>();
         if(sysUser != null) {
             userId = sysUser.getUserId();
             if(StringUtil.isNotBlank(userId)){
                 objectList = sysResService.getMenuList(userId);
             }

         }
         //查询资源内容

        
//        List<Object> objectList = (List<Object>) WebUtils.getSessionAttribute(request, Constants.SESSION_MENU_LIST);
        
        return AjaxResult.success(objectList);
    }
    
}
